/**
 * SEO 工具函数
 */

/**
 * 生成产品结构化数据 (Schema.org Product)
 */
export function generateProductSchema(product, systemSettings = {}) {
  if (!product) return null

  const schema = {
    '@context': 'https://schema.org',
    '@type': 'Product',
    name: product.name,
    description: product.description || product.short_description,
    image: product.images?.[0]?.image_path || '',
    sku: product.sku,
    brand: {
      '@type': 'Brand',
      name: product.brand || systemSettings.site_name || 'Orange Tech'
    }
  }

  // 添加价格信息
  if (product.price) {
    schema.offers = {
      '@type': 'Offer',
      price: product.price,
      priceCurrency: 'CNY',
      availability: product.stock_quantity > 0 
        ? 'https://schema.org/InStock' 
        : 'https://schema.org/OutOfStock',
      url: window.location.href
    }
  }

  // 添加评分（如果有）
  if (product.rating && product.review_count) {
    schema.aggregateRating = {
      '@type': 'AggregateRating',
      ratingValue: product.rating,
      reviewCount: product.review_count
    }
  }

  return JSON.stringify(schema)
}

/**
 * 生成面包屑结构化数据
 */
export function generateBreadcrumbSchema(breadcrumbs) {
  if (!breadcrumbs || breadcrumbs.length === 0) return null

  const itemListElement = breadcrumbs.map((item, index) => ({
    '@type': 'ListItem',
    position: index + 1,
    name: item.name || item,
    item: item.url || window.location.origin
  }))

  const schema = {
    '@context': 'https://schema.org',
    '@type': 'BreadcrumbList',
    itemListElement
  }

  return JSON.stringify(schema)
}

/**
 * 生成组织结构化数据
 */
export function generateOrganizationSchema(systemSettings = {}) {
  return JSON.stringify({
    '@context': 'https://schema.org',
    '@type': 'Organization',
    name: systemSettings.site_name || 'Orange Tech',
    url: window.location.origin,
    logo: systemSettings.site_logo ? `${window.location.origin}${systemSettings.site_logo}` : `${window.location.origin}/images/logo.png`,
    contactPoint: {
      '@type': 'ContactPoint',
      telephone: systemSettings.contact_phone || '+86-400-666-8800',
      contactType: 'customer service',
      areaServed: 'CN',
      availableLanguage: 'zh-CN'
    },
    sameAs: [
      // 社交媒体链接
    ]
  })
}

/**
 * 生成搜索框结构化数据
 */
export function generateSearchActionSchema() {
  return JSON.stringify({
    '@context': 'https://schema.org',
    '@type': 'WebSite',
    url: window.location.origin,
    potentialAction: {
      '@type': 'SearchAction',
      target: {
        '@type': 'EntryPoint',
        urlTemplate: `${window.location.origin}/search?keyword={search_term_string}`
      },
      'query-input': 'required name=search_term_string'
    }
  })
}

/**
 * 截断描述文本
 */
export function truncateDescription(text, maxLength = 160) {
  if (!text) return ''
  if (text.length <= maxLength) return text
  return text.substring(0, maxLength - 3) + '...'
}

/**
 * 生成规范URL
 */
export function generateCanonicalUrl(path = '') {
  const origin = window.location.origin
  return path ? `${origin}${path}` : window.location.href.split('?')[0]
}

